<?php

namespace App\Console\Commands;

use App\Repositories\Contracts\PolicyUploadInterface;
use Illuminate\Console\Command;

class HbGjPolicyCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'hbgj:policyRepeatDelete';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle(PolicyUploadInterface $policyUploadInterface)
    {
        //

        while (true) {

            $policies = $policyUploadInterface->get(['status'=>1],'*',[],'id','desc');

            if ($policies->isNotEmpty()) {

                foreach ($policies as $key=>$value) {

                    $param =[
                        ['id','<',$value['id']],
                        ['depart_city_code','=',$value['depart_city_code']],
                        ['arrived_city_code','=',$value['arrived_city_code']],
                        ['date','=',$value['date']],
                        ['flight_code','=',$value['flight_code']],
                        ['share_flight_code','=',$value['share_flight_code']],
                        ['status','=',1]
                    ];
                    $policy = $policyUploadInterface->index($param);
                    if ($policy) {

                        $specialPolicyInterface = resolve('App\Repositories\Ota\Ctrip\Policy\Contracts\SpecialPolicyInterface');
                        $res = $specialPolicyInterface->delete([$value['policy_id']]);var_dump($res);

                        if (isset($res['TradeResult']) && isset($res['TradeResult']['Result']) && (!empty($res['TradeResult']['Result']['SuccessList']) ||  !empty($res['TradeResult']['Result']['FailedList']))) {

                            $data['status'] = 3;
                            $policyUploadInterface->update(['id'=>$policy['id']],$data);
                        }

                    }
                }
            }

        }

    }
}
